package com.gx.web;

import com.gx.dto.Exposer;
import com.gx.dto.SeckillExecution;
import com.gx.dto.SeckillResult;
import com.gx.entity.Seckill;
import com.gx.enums.SeckillStatEnum;
import com.gx.exception.RepeatKillException;
import com.gx.exception.SeckillCloseException;
import com.gx.service.SeckillService;
import com.gx.service.impl.SeckillServiceimpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by gx on 2017/2/10.
 */
@Controller
@RequestMapping("/seckill")//url:模块/资源/{}/细分
public class SeckillController {
    private static final Logger logger = LoggerFactory.getLogger(SeckillServiceimpl.class);

    @Resource
    SeckillService seckillService;

    /**
     * 查询秒杀商品列表
     * @param model
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(Model model){
        List<Seckill> list = seckillService.getSeckillList();
        model.addAttribute("list",list);
        return "list";
    }


    @RequestMapping(value = "/{seckillId}/detail",method = RequestMethod.GET)
    public String detail(@PathVariable("seckillId") long seckillId,Model model){
        if (StringUtils.isEmpty(seckillId)){
            return "redirect:/seckill/list";
        }
        Seckill seckill = seckillService.getById(seckillId);
        if (seckill==null){
         return "redirect:/seckill/list";
        }
        model.addAttribute("seckill",seckill);
        return "detail";
    }


    /**
     * 输出秒杀地址
     * @param seckillId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{seckillId}/exposer",method = RequestMethod.POST,
                    produces = {"application/json;charset=UTF-8"})
    public SeckillResult<Exposer>   export(@PathVariable("seckillId") Long seckillId ){
        SeckillResult result;
        try {
            Exposer exposer = seckillService.exportSeckillUrl(seckillId);
            result = new SeckillResult<Exposer>(true,exposer);

        }catch (Exception e){
            logger.error(e.getMessage(),e);
            result = new SeckillResult<Exposer>(false,e.getMessage());
        }
        return  result;
    }


    /**
     * 执行秒杀请求
     * @param seckillId
     * @param md5
     * @param uerPhone
     * @return
     */

    @ResponseBody
    @RequestMapping(value = "/{seckillId}/{md5}/execution",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    public SeckillResult<SeckillExecution> execute(@PathVariable("seckillId") Long seckillId,
                                                   @PathVariable("md5") String md5,
                                                   @CookieValue(value = "killPhone",required = false) Long uerPhone){
        if (StringUtils.isEmpty(uerPhone)){
            return new SeckillResult<SeckillExecution>(false,"未注册");
        }
        SeckillResult<SeckillExecution> result;
        try {
            SeckillExecution execution = seckillService.executeSeckill(seckillId,uerPhone,md5);
            result = new SeckillResult<SeckillExecution>(true,execution);
        }catch (RepeatKillException e1) {
            logger.error(e1.getMessage(), e1);
            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.REPEAT_KILL);
            result = new SeckillResult<SeckillExecution>(true,execution);
        }catch (SeckillCloseException e2) {
            logger.error(e2.getMessage(), e2);
            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.END);
            result = new SeckillResult<SeckillExecution>(true,execution);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.INNER_ERROR);
            result = new SeckillResult<SeckillExecution>(true,e.getMessage());
        }
        return  result;
    }


    /**
     * 查询系统时间
     * @return
     */

    @RequestMapping(value = "/time/now",method = RequestMethod.GET)
    @ResponseBody
    public SeckillResult<Long> time(){
        Date date = new Date();
        return new SeckillResult<Long>(true,date.getTime());
    }



}
